package simple;
//简单模式

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import contants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 生产者代码编写
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接Connection
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);//主机ip
        factory.setPort(Constants.PORT);//端口号
        factory.setUsername(Constants.USER_NAME);//用户名
        factory.setPassword(Constants.PASSWORD);//用户密码
        factory.setVirtualHost(Constants.VIRTUAL_HOST);//虚拟主机
        Connection connection = factory.newConnection();
        //2.开启信道Channel
        Channel channel = connection.createChannel();
        //3.指定队列
        /*
            queueDeclare(String queue, boolean durable, boolean exclusive,
            boolean autoDelete, Map<String, Object> arguments)
            1.queue: 队列名称
            2.durable: 是否持久化, 当mq重启之后, 消息还在
            3.exclusive:是否独占, 只能有⼀个消费者监听队列,当Connection关闭时, 是否删除队列
            4.autoDelete: 是否⾃动删除, 当没有Consumer时, ⾃动删除掉
            5.arguments: ⼀些参数
         */
        channel.queueDeclare("queue01", true, false, false, null);
        //4.发送信息:第二个参数为routingkey，和队列名称一致
        channel.basicPublish("", "queue01", null, "Hello World".getBytes());
        //5.资源释放
        channel.close();
        connection.close();
    }

}
